﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CheckUseCase.cs" company="">
//   
// </copyright>
// <summary>
//   The check use case.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.UseCases
{
    using System;

    /// <summary>
    ///   The check use case.
    /// </summary>
    public abstract class CheckUseCase : UseCase, ICheckUseCase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CheckUseCase"/> class.
        /// </summary>
        /// <param name="parent">
        /// The parent. 
        /// </param>
        /// <param name="service">
        /// The service. 
        /// </param>
        public CheckUseCase(ICompoundUseCase parent, IUseCaseInstanceService service)
            : base(parent, service)
        {
        }

        #endregion

        #region Public Events

        /// <summary>
        ///   The is checked changed.
        /// </summary>
        public event Action<ICheckUseCase> IsCheckedChanged;

        #endregion

        #region Public Properties

        /// <summary>
        ///   Gets or sets a value indicating whether is checked.
        /// </summary>
        public bool IsChecked
        {
            get
            {
                return GetValue();
            }

            set
            {
                if (GetValue() == value)
                {
                    return;
                }

                SetValue(value);
                OnIsCheckedChanged();
                Modified = true;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        ///   The get value.
        /// </summary>
        /// <returns> The <see cref="bool" /> . </returns>
        protected abstract bool GetValue();

        /// <summary>
        ///   The on is checked changed.
        /// </summary>
        protected virtual void OnIsCheckedChanged()
        {
            Modified = true;

            if (IsCheckedChanged != null)
            {
                IsCheckedChanged(this);
            }
        }

        /// <summary>
        /// The set value.
        /// </summary>
        /// <param name="value">
        /// The value. 
        /// </param>
        protected abstract void SetValue(bool value);

        #endregion
    }
}